<?php

class CF_Sorter {

	var $fieldspec;
	var $tablename;

// Sort variables
	var $new_order;
	var $cur_order;
	var $pagename;
	var $new_dir;
	var $cur_dir;
	var $orderstring;
	var $ordervar = 'order';	
	
	
	function CF_Sorter($fieldspec, $tablename){
		$this->tablename = $tablename;
		$this->fieldspec = $fieldspec;
	
	}
	
	
	/**
	 *
     * get_sort_orders  Method will recieve sort order from $_REQUEST or $_SESSION
     * variables and set the internal variables for use in queries
	 * (Future) - implement this using GetPersistent
	 */
	function get_sort_orders(){
		if(isset($_SESSION[$this->tablename . '_order']) AND $_SESSION[$this->tablename . '_order'] != ''){
			$this->cur_order = $_SESSION[$this->tablename . '_order'];
			$this->cur_dir = $_SESSION[$this->tablename . '_dir'];
			
			$this->orderstring = " ORDER BY $this->cur_order  $this->cur_dir "; 
			
			}
		if(isset($_REQUEST['order']) AND $_REQUEST['order'] != ''){
			$this->new_order = $_REQUEST['order'];
			if($this->new_order == $this->cur_order){ // flip direction if clicked twice
				if($this->cur_dir == 'DESC'){
					$this->cur_dir = 'ASC';
				}
				else{
					$this->cur_dir = 'DESC';
				}
			}// End same order clicked twice
			else{
				$this->cur_order = $this->new_order;
				$this->cur_dir = 'ASC'; // default to asc
			}
		$_SESSION[$this->tablename . '_order'] = $this->cur_order;
		$_SESSION[$this->tablename . '_dir'] = $this->cur_dir;
		
		$this->orderstring = " ORDER BY $this->cur_order  $this->cur_dir "; 
		
	
		} // end order in request
	} // end get sort order


		/**
	 *  make_query_sort  Method will show the 'ORDER BY' query piece.  For Mysql, so far
	 */
	function make_query_sort(){
		$this->get_sort_orders();
		return $this->orderstring;
	
	}

	function printstuff(){
		$tn = $this->tablename;
		print "Self Page Is {$this->tablename} <pre>";
		print_r($this->fieldspec);
		print "</pre>";
	
	
	}
	
	
}


?>